package com.ssl.rms.util;

import com.google.common.collect.Lists;
import com.ssl.rms.entity.Menu;
import com.ssl.rms.entity.TreeNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 描述：树形节点工具
 *
 * @author ssl
 * @create 2018/04/13 14:43
 */
public class TreeNodeUtils {
    private static Logger logger = LoggerFactory.getLogger(TreeNodeUtils.class);

    public static List<TreeNode> getTreeNode(List<Menu> menus) {
        List<TreeNode> treeNodes = Lists.newArrayList();
        TreeNode treeNode = null;
        for (Menu menu : menus) {
            treeNode = new TreeNode();
            treeNode.setKey(menu.getId());
            treeNode.setTitle(menu.getName());
            treeNode.setSort(menu.getSort());
            /** 父级菜单id为null，代表该菜单为一级菜单 */
            if (null == menu.getParentId()) {
                treeNodes.add(treeNode);
            }else {
                treeNode.setParentId(menu.getParentId());
            }
        }
        /** 排序 */
        sortListAsc(treeNodes);
        /** 给一级菜单设置子菜单 */
        for (TreeNode tn : treeNodes) {
            tn.setChildren(getTreeNodeChild(tn.getKey(), menus));
        }
        return treeNodes;
    }

    /**
     * 递归查询子菜单
     *
     * @param id        当前菜单id
     * @param rootMenus 要查找的列表
     * @return
     */
    private static List<TreeNode> getTreeNodeChild(Long id, List<Menu> rootMenus) {
        List<TreeNode> childList = Lists.newArrayList();
        TreeNode treeNode = null;
        for (Menu menu : rootMenus) {
            treeNode = new TreeNode();
            treeNode.setKey(menu.getId());
            treeNode.setTitle(menu.getName());
            treeNode.setSort(menu.getSort());
            /** 遍历所有节点，将父菜单id与传过来的id比较 */
            if (null != menu.getParentId() && id.equals(menu.getParentId())) {
                childList.add(treeNode);
            }
        }
        /** 如果子菜单没有了，则返回null */
        if (0 == childList.size()) {
            return null;
        }
        /** 排序 */
        sortListAsc(childList);
        /** 循环子菜单的子菜单 */
        for (TreeNode tn : childList) {
            /** 递归获取子菜单 */
            tn.setChildren(getTreeNodeChild(tn.getKey(), rootMenus));
        }
        return childList;
    }

    /**
     * 按照sort属性进行降序排列:值大的排在前面
     *
     * @param treeNodeList
     */
    private static void sortListDesc(List<TreeNode> treeNodeList) {
        Collections.sort(treeNodeList, (o1, o2) -> {
            /** 按照sort字段进行降序排列 */
            if (o1.getSort() > o2.getSort()) {
                return -1;
            }
            if (Objects.equals(o1.getSort(), o2.getSort())) {
                return 0;
            }
            return 1;
        });
    }

    /**
     * 按照sort属性进行升序排列：值小的排在前面
     *
     * @param treeNodeList
     */
    private static void sortListAsc(List<TreeNode> treeNodeList) {
        Collections.sort(treeNodeList, (o1, o2) -> {
            if (o1.getSort() < o2.getSort()) {
                return -1;
            }
            if (Objects.equals(o1.getSort(), o2.getSort())) {
                return 0;
            }
            return 1;
        });
    }
}
